<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" >
      <el-form-item label="编号" prop="number">
        <el-input v-model="form.numberNum" disabled style="width: 3.55rem" />
      </el-form-item>
      <el-form-item label="请假类型" prop="leaveType">
        <el-select
          :disabled="isdisabled"
          style="width: 3.55rem"
          v-model="form.leaveType"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in leaveTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker
          clearable
          style="width: 3.55rem"
          v-model="form.startDate"
          type="date"
          @change="dateChanged"
          :picker-options="pickerOptionStart"
          :disabled="isdisabled"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker
          clearable
          style="width: 3.55rem"
          v-model="form.endDate"
          type="date"
          @change="dateChanged"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptionEnd"
          :disabled="isdisabled"
          placeholder="请选择结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="请假时长" prop="leaveCount">
        <el-input
          v-model="form.leaveCount"
          placeholder="请假时长必须大于0"
          :disabled="true"
          style="width: 3.55rem"
        >
          <i slot="suffix" style="font-style:normal;">天</i>
        </el-input>
      </el-form-item>
      <el-form-item label="请假事由" prop="leaveReason">
        <el-input
          :disabled="isdisabled"
          v-model="form.leaveReason"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
      <el-form-item label="附件">
        <file-upload v-model="form.attachment" :is-disabled="isdisabled" />
      </el-form-item>
    </el-form>
    <div v-show="parentTaskId==null" class="dialog-footer" style="text-align:center">
      <el-button :loading="buttonLoading" size="small" type="info" @click="submitCallback">关闭</el-button>
      <el-button
        :loading="buttonLoading"
        size="small"
        type="primary"
        @click="submitForm"
        v-if="confirmVisible"
      >提交</el-button>
    </div>
    <!-- 工作流开始 -->
    <verify
      ref="verifyRef"
      :taskId="taskId"
      @submitCallback="submitCallback"
      :componentName="componentName"
      :taskVariables="taskVariables"
      :sendMessage="sendMessage"
    ></verify>
  </div>
</template>

<script>
import { getHumanLeave } from "@/api/oa/humanLeave";
import verify from "@/components/Process/Verify";
import { parseTime } from "@/utils/ruoyi";
import dayjs from "dayjs";
export default {
  name: "HumanLeaveForm",
  components: {
    verify
  },
  props: {
    businessKey: String, // 业务唯一标识
    parentTaskId: String, // 父级任务id
    taskId: String, // 任务id
    confirmVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      componentName: "请假申请",
      isdisabled: true,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 表单参数
      form: {},
      projectNameList: [],
      // 表单校验
      rules: {
        leaveType: [
          { required: true, message: "请假类型不能为空", trigger: "change" }
        ],
        startDate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        leaveReason: [
          { required: true, message: "请假时间不能为空", trigger: "blur" }
        ]
      },
      taskVariables: undefined,
      //消息提醒
      sendMessage: {},
      processKey: "oa_human_leave",
      // 出差日期的可选范围
      pickerOptionStart: {
        disabledDate: time => {
          if (this.form.endDate) {
            return time.getTime() > new Date(this.form.endDate).getTime();
          }
          return time.getTime() <= new Date();
        }
      },
      pickerOptionEnd: {
        disabledDate: time => {
          if (this.form.startDate) {
            return time.getTime() < new Date(this.form.startDate).getTime();
          }
          return time.getTime() <= new Date();
        }
      }
    };
  },
  watch: {
    businessKey: {
      immediate: true, // 很重要！！！
      handler(newVal) {
        if (newVal) {
          this.getById();
        }
      }
    }
  },
  methods: {
    submitCallback() {
      this.$emit("closeForm");
    },
    async getById() {
      const { data } = await getHumanLeave(this.businessKey);
      this.form = data;
      if (data.actBusinessStatus.createTime) {
        this.form.numberNum =
          this.processKey +
          "@" +
          parseTime(data.actBusinessStatus.createTime, "{y}{m}{d}{h}{i}{s}");
      }
    },
    dateChanged() {
      if (this.form.startDate != null && this.form.endDate != null) {
        let val = dayjs(this.form.endDate).diff(
          dayjs(this.form.startDate),
          "days"
        );
        this.$set(this.form, "leaveCount", val < 0 ? val : val + 1);
        console.log(this.form);
      }
    },
    /** 提交按钮 */
    submitForm() {
      getHumanLeave(this.businessKey).then(response => {
        this.taskVariables = {
          entity: response.data
        };
        this.sendMessage = {
          title: "请假申请审批提醒",
          messageContent: this.$store.state.user.nickName + "审批通过了请假申请"
        };
      });
      this.$refs.verifyRef.visible = true;
      this.$refs.verifyRef.reset();
    }
  }
};
</script>
<style scoped>
/* 设置滚动条宽度和高度 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 8px; /* 横向滚动条 */
  height: 8px; /* 纵向滚动条 必写 */
}
/* 设置滚动条样式 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #dde;
  border-radius: 3px;
}
#selectForm >>> .el-form-item__label {
  font-size: 12px;
}
::v-deep .bjsm .el-form-item__content {
  width: calc(100% - 130px) !important;
}
</style>
